package service

import (
	"fmt"
	"hanyun-admin/src/entity"
	"log"
)
//获取字典数据集
func SelectDictDataList(dictData entity.DictData)[]entity.DictData{
	dictDataList := make([]entity.DictData,0)
	sql := DbEngine.Select("*")
	if dictData.DictType != "" {
		sql.Where("dict_type = ?",dictData.DictType)
	}
	if dictData.DictLabel != "" {
		sql.Where("dict_label = ?",dictData.DictLabel)
	}
	if dictData.Status != "" {
		sql.Where("status = ?",dictData.Status)
	}
	err := sql.Limit(dictData.PageSize,(dictData.PageNum - 1)*dictData.PageSize).Find(&dictDataList)
	if err != nil {
		log.Println("select dictData error ",err)
	}
	return dictDataList
}
//获取字段数据集长度
func SelectDictDataListCount(dictData entity.DictData)int{
	sql := DbEngine.Select("count(0)")
	if dictData.DictType != "" {
		sql.Where("dict_type = ?",dictData.DictType)
	}
	if dictData.DictLabel != "" {
		sql.Where("dict_label = ?",dictData.DictLabel)
	}
	if dictData.Status != "" {
		sql.Where("status = ?",dictData.Status)
	}
	count,err := sql.Count(entity.DictData{})
	if err != nil {
		log.Println("select dict data list count error",err)
	}
	return int(count)
}
//根据字典类型查询字典数据
func SelectDictDataByType(dictType string)[]entity.DictData{
	dictData := make([]entity.DictData,0)
	err := DbEngine.Where("dict_type = ?",dictType).Find(&dictData)
	if err != nil {
		fmt.Println("select dictData by dictType error ",err)
	}
	return dictData
}
//新增字典数据
func InsertDictDate(dictData entity.DictData)error{
	_,err := DbEngine.Insert(dictData)
	return err
}
//详情
func SelectDictDataByDictCode(dictCode int64)entity.DictData{
	var dictData entity.DictData
	_,err := DbEngine.Id(dictCode).Get(&dictData)
	if err != nil {
		log.Println("select dict data by dictCode error ",err)
	}
	return dictData
}
//修改
func UpdateDictData(dictData entity.DictData)error{
	_,err := DbEngine.Id(dictData.DictCode).Update(dictData)
	return err
}
//删除
func DeleteDictData(dictCode int64)error{
	_,err := DbEngine.Id(dictCode).Delete(entity.DictData{})
	return err
}